package com.example.jn_dxs.Utlis;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.jn_dxs.R;


public class PopWindowUtil {
    private static PopWindowUtil instance;
    private PopupWindow mPopupWindow;
    private static Animation arrowDown, arrowUp;
    private static Context mContext;

    public static PopWindowUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (PopWindowUtil.class) {
                if (instance == null) {
                    instance = new PopWindowUtil();
                }
                mContext = context;
                arrowDown = AnimationUtils.loadAnimation(context, R.anim.arrow_down);
                arrowUp = AnimationUtils.loadAnimation(context, R.anim.arrow_up);
            }
        }
        return instance;
    }

    public void setDismiss() {
        mPopupWindow.dismiss();
    }

    public void popupCrater(ImageView rightTextView, View view, View anchor) {
        if (rightTextView.getTag(rightTextView.getId()) == null || ((int) rightTextView.getTag(rightTextView.getId())) == 1) {
            rightTextView.startAnimation(arrowDown);
            PopWindowUtil.getInstance(mContext).setOnDismissListener(new OnDismissListener() {
                @Override
                public void dismiss() {
                    rightTextView.startAnimation(arrowUp);
                    rightTextView.setTag(rightTextView.getId(), 1);
                }
            });
            PopWindowUtil.getInstance(mContext).makePopupWindow(view).showLocationWithAnimation(anchor, 0, -50);
        } else {
            rightTextView.startAnimation(arrowUp);
            rightTextView.setTag(rightTextView.getId(), 1);
        }
    }

    /**
     * @param content 传入内容的view
     * @return
     */
    public PopWindowUtil makePopupWindow(View content) {
        mPopupWindow = new PopupWindow(mContext);
        mPopupWindow.setBackgroundDrawable(content.getResources().getDrawable(R.color.lv_cacheColorHint));
        mPopupWindow.setWidth(320); //宽度
        mPopupWindow.setHeight(500);  //高度
        mPopupWindow.setContentView(content);
        mPopupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
        mPopupWindow.setTouchable(true); // 设置PopupWindow可触摸
        mPopupWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        return instance;
    }

    /**
     * @param anchor   锚点View
     * @param xOff     距离view的x轴偏移量
     * @param yOff     距离view的y轴偏移量
     * @return
     */
    public PopupWindow showLocationWithAnimation(View anchor, int xOff, int yOff) {
        mPopupWindow.showAsDropDown(anchor, xOff, yOff);
        mPopupWindow.setOnDismissListener(() -> {
            if (mListener != null) {
                mListener.dismiss();
            }
        });
        return mPopupWindow;
    }

    public interface OnDismissListener {
        void dismiss();
    }

    private OnDismissListener mListener;

    public void setOnDismissListener(OnDismissListener listener) {
        mListener = listener;
    }
}
